﻿using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.IO;

using System.Threading;
using Net.Properties;




namespace Net.Class
{

    /// <summary>   

    /// WriteData 的摘要说明。   

    /// </summary>   

    public class Log
    {

        static public string m_Path = @".\Log\";

        static public string m_FilePreName = "AppLog";

        static Mutex m_WriteMutex = new Mutex();





        public Log()
        {

            //   

            // TODO: 在此处添加构造函数逻辑   

            //   



        }





        static public bool WriteLine(string tile,string content)
        {
            if (!Settings.Default.isLogEnabled) return false;
            return WriteLine("["+tile+"]["+content+"]");

        }


        /// <summary>
        /// 获取文件大小
        /// </summary>
        /// <param name="sFullName"></param>
        /// <returns></returns>
        public static long GetFileSize(string sFullName)
        {
            long lSize = 0;
            if (File.Exists(sFullName))
                lSize = new FileInfo(sFullName).Length;
            return lSize;
        }
        /// 计算文件大小函数(保留两位小数),Size为字节大小
        /// </summary>
        /// <param name="Size">初始文件大小</param>
        /// <returns></returns>
        public static string CountSize(long Size)
        {
            string m_strSize = "";
            long FactSize = 0;
            FactSize = Size;
            if (FactSize < 1024.00)
                m_strSize = FactSize.ToString("F2") + " Byte";
            else if (FactSize >= 1024.00 && FactSize < 1048576)
                m_strSize = (FactSize / 1024.00).ToString("F2") + " K";
            else if (FactSize >= 1048576 && FactSize < 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00).ToString("F2") + " M";
            else if (FactSize >= 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00 / 1024.00).ToString("F2") + " G";
            return m_strSize;
        }

        /// <summary>
        /// 获取一批文件的大小
        /// </summary>
        /// <param name="sFilePath">文件所在的路径</param>
        /// <param name="sMask">文件名称含通配符</param>
        /// <returns></returns>
        public static long GetFilesSize(string sFilePath, string sMask)
        {
            long lSize = 0;
            if (sMask.Trim() == "")
                return lSize;
            DirectoryInfo pDirectoryInfo = new DirectoryInfo(sFilePath);
            if (pDirectoryInfo.Exists == false)
                return lSize;
            FileInfo[] pFileInfos = pDirectoryInfo.GetFiles(sMask, SearchOption.TopDirectoryOnly);
            foreach (FileInfo e in pFileInfos)
            {
                lSize += GetFileSize(e.FullName);
            }
            return lSize;
        }


        static public bool WriteLine(string dataText)//, int theLevel)
        {
            if (!Settings.Default.isLogEnabled) return false;
            FileStream fs = null;

            StreamWriter sw = null;

            bool ret = true;

            m_WriteMutex.WaitOne();

            try
            {

                string FileName = m_Path;

                //CHECK文件目录存在不   

                if (!Directory.Exists(FileName))
                {

                    Directory.CreateDirectory(FileName);

                }

                FileName += @"\" + m_FilePreName +".log";// DateTime.Now.ToString("-yyyMMdd")+".log";

                //CHECK文件存在不   

                if (!File.Exists(FileName))
                {

                    FileStream tempfs = File.Create(FileName);

                    tempfs.Close();

                }
                else
                {

                    long lSize = new FileInfo(FileName).Length;
                    if (lSize > 1024 * 1024 * 2) //大小>2M,新建一个文件
                    {
                        try
                        {
                            File.Delete(FileName);
                            FileStream tempfs = File.Create(FileName);
                            tempfs.Close();
                        }
                        catch //(Exception ex)
                        {
                            return false;
                        }

                    }
                }
                fs = new FileStream(

                    FileName,

                    FileMode.Append,

                    FileAccess.Write,

                    FileShare.None);
                


                fs.Seek(0, System.IO.SeekOrigin.End);

                sw = new StreamWriter(fs, System.Text.Encoding.UTF8);

                string LineText = "[" + DateTime.Now.ToString("yyy-MM-dd ") + DateTime.Now.ToString("T") + "] " + dataText ;

                sw.WriteLine(LineText);

                if (sw != null)
                {

                    sw.Close();

                    sw = null;

                }

                if (fs != null)
                {

                    fs.Close();

                    fs = null;

                }





            }

            catch (Exception)
            {

                ret = false;

            }

            finally
            {

                try
                {

                    if (sw != null)
                    {

                        sw.Close();

                        sw = null;

                    }

                    if (fs != null)
                    {

                        fs.Close();

                        fs = null;

                    }

                }

                catch
                {

                }

                m_WriteMutex.ReleaseMutex();

            }

            return ret;

        }



        static public void WriteFile(string filename, byte[] dataText)
        {

            try
            {

                FileInfo finfo = new FileInfo(filename);

                FileStream fs = finfo.OpenWrite();

                fs.Write(dataText, 0, dataText.Length);

                fs.Close();

                return;

            }

            catch (Exception)
            {

                return;

            }

        }

    }



}